define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
 
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/index' + location.search,
                    add_url: 'shop/add',
                    edit_url: 'shop/edit',
                    del_url: 'shop/del',
                    multi_url: 'shop/multi',
                    import_url: 'shop/import',
                    table: 'shop',
                }
            });
 
            var table = $("#table");
 
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('ID')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'description', title: __('Description'), operate: 'LIKE'},
                        // {field: 'notice', title: __('Notice'), operate: 'LIKE'},
                        // {field: 'province', title: __('Province'), operate: 'LIKE'},
                        // {field: 'city', title: __('City'), operate: 'LIKE'},
                        // {field: 'area', title: __('Area'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'workrange', title: __('Workrange'), operate: 'LIKE'},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        // {field: 'user.username', title: __('用户')},
                        {field: 'order_num', title: __('今日订单量')},
                        {field: 'moneyCount', title: __('今日收入')},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'state', title: __('审核状态'), searchList: {"0":__('审核中'),"1":__('审核成功'),"2":__('审核失败')}, formatter: Table.api.formatter.status},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE'},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        buttons: [
                            {
                                name: 'user',
                                title: '查看用户',
                                text: '查看用户',
                                classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                url: 'shop_user/index',//接口地址 控制器名/方法名
                            },
                            {
                                name: 'print',
                                title: '明细',
                                text: '明细',
                                classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                url: 'user/mony_log/index',//接口地址 控制器名/方法名
                            },
                            {
                                name: 'update',
                                title: '审核',
                                text: '审核',
                                classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                url: 'shop/update',//接口地址 控制器名/方法名
                                visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                    if (row.state != '1') {
                                        return true;//显示
                                    } else {
                                        return false;//隐藏
                                    }
 
                                },
                            },
                        ]}
                    ]
                ]
            });
 
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });
 
            var table = $("#table");
 
            // 初始化表格
            table.bootstrapTable({
                url: 'shop/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shop/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shop/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
 
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
 
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        update: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});